﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Sbook.AdminIstrator
{
    public partial class delivery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    string User = "";
                    if (Request.QueryString["DeliveryId"] != null)
                    {
                        string Id = Request.QueryString["DeliveryId"];
                        //try
                        //{
                            BindDeliveryById(Id);
                            BindUom();
                        //}
                        //catch (Exception)
                        //{


                        //}
                        
                    }
                    
                }
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
        }

        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }

        private void BindDeliveryById(string Id)
        {
            rpDeliveryDetail.DataSource = Business.DeliveryDetailService.DeliveryDetail_GetByTop("", "dt.DeliveryId=" + Id, "");
            rpDeliveryDetail.DataBind();
            int sumQty = 0;
            int sumPrice = 0;
            lblPoCode.Text = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryId == Business.DeliveryDetailService.DeliveryDetail_GetByAll().Find(da => da.DeliveryId == Id).DeliveryId).DeliveryCode;
            for (int i = 0; i < rpDeliveryDetail.Items.Count; i++)
            {
                Label lblPrice = (Label)rpDeliveryDetail.Items[i].FindControl("lblPrice");
                lblPrice.Text = Format_Price(lblPrice.Text.Replace(".0000", ""));

                Label lblQty = (Label)rpDeliveryDetail.Items[i].FindControl("lblQty");

                Label lblTotalPrice = (Label)rpDeliveryDetail.Items[i].FindControl("lblTotalPrice");
                lblTotalPrice.Text = (Convert.ToInt32(lblPrice.Text.Split('.')[0]) * Convert.ToInt32(lblQty.Text)).ToString()+".000";
                string temp = lblTotalPrice.Text.Split('.')[0];
                //lblTotalPrice.Text = Format_Price(lblTotalPrice.Text.Replace(".0000",""));

                sumQty += Convert.ToInt32(lblQty.Text);
                if (temp != "")
                {
                    sumPrice += Convert.ToInt32(temp);
                }

               
            }
            Label lblSumQty = (Label)rpDeliveryDetail.Controls[0].Controls[0].FindControl("lblSumQty");
            Label lblSumPrice = (Label)rpDeliveryDetail.Controls[0].Controls[0].FindControl("lblSumPrice");
            lblSumQty.Text = sumQty.ToString();
            lblSumPrice.Text = sumPrice.ToString()+".000";

            lblShippingPoint.Text = Business.DeliveryService.Delivery_GetByTop("","DeliveryId=" +Id,"")[0].DeliveryShippingPoint;
            lblDeliveryTime.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryDate;

            if (Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].UId != "")
            {
                List<Data.UserInfo> lstUs = Business.UserService.User_GetById(Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].UId);
                lblCustomerName.Text = lstUs[0].Fullname;
                lblAddress.Text = lstUs[0].Address;
               
                lblUniversity.Text = lstUs[0].University;
                lblEmail.Text = lstUs[0].Email;
                lblPhone.Text = lstUs[0].Phone;
            }
            else
            {
                lblCustomerName.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryName;
                lblAddress.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryAdd;
               
                lblUniversity.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryUniversity;
                lblEmail.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryEmail;
                lblPhone.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryPhone;
            }
        }

        // method get User's email by DeliveryID
        public string GetEmailByDeliveryId(object Id)
        {
            if(Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id.ToString(), "")[0].UId != "")
                return (Business.UserService.User_GetById(Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id.ToString(), "")[0].UId))[0].Email;
            else
                return Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryEmail;
        }

        protected void lbtPrint_Click(object sender, EventArgs e)
        {
            ArrayList lstUom = new ArrayList();
            for (int i = 0; i < rpDeliveryDetail.Items.Count; i++)
            {
                DropDownList drdl = (DropDownList)rpDeliveryDetail.Items[i].FindControl("drdlUom");
                lstUom.Add(drdl.SelectedItem.Text);
            }
            Session["Uom"] = lstUom;
            Response.Redirect("DeliveryDetail_Print.aspx?DeliveryId="+Request.QueryString["DeliveryId"]);
        }

        

        private void BindUom()
        {
            for (int i = 0; i < rpDeliveryDetail.Items.Count; i++)
            {
                DropDownList drdl = (DropDownList)rpDeliveryDetail.Items[i].FindControl("drdlUom");
                drdl.DataValueField = "UomId";
                drdl.DataTextField = "UomName";
                drdl.DataSource = Business.UomService.Uom_GetByAll();
                drdl.DataBind();
            }
        }
    }
}